/***************************************************************************
 *            objects_manager
 *
 *  Wed Sep 17 22:44:48 2008
 *  Copyright  2008  Tiago Falcão
 *  <developer@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include "objects_manager.h"

//Cria Estrutura
objects_manager * objects_manager_create(){
	objects_manager * objs=calloc(1,sizeof(objects_manager)); //Aloca Estrutura
	return objs;									//Retorna Estrtura
}

//Deleta Estrutura
void objects_manager_delete(objects_manager * objs){
	int i;
	for(i=0;i<objs->objs_qtd;i++)			//Deleta Todos Objetos
		object_delete(objs->objs[i]);
	free(objs->objs);						//Deleta Vetor
	free(objs);								//Deleta Estrutura
}

//Display Objetos da Lista
void objects_manager_display(objects_manager * objs){
	int i;
	if(objs->selected)
			object_display(objs->selected);
	for(i=0;i<objs->objs_qtd;i++)
		if(objs->objs[i]!=objs->selected)
			object_display(objs->objs[i]);
}

//Adiciona Objeto a Lista
object * objects_manager_add(objects_manager * objs,void (*display)(void *,object *), void * o,void (*object_delete)(void *)){
	objs->objs=realloc(objs->objs,(++objs->objs_qtd)*sizeof(object *));		//Aumenta Lista
	objs->objs[objs->objs_qtd-1]=object_create(display,o,object_delete);	//Cria Objeto3D
	return objs->objs[objs->objs_qtd-1];									//Retorna Ponteiro para Objeto
}

//Deleta Objeto Marcado
void objects_manager_delete_selected(objects_manager * objs){
	if(objs->selected){
		int i=objs->selected_index;
		object_delete(objs->selected);
		while(i<objs->objs_qtd){
			objs->objs[i]=objs->objs[i+1];
			i++;
		}
		objs->objs=realloc(objs->objs,(--objs->objs_qtd)*sizeof(object *));		//Diminui Lista
		objs->selected=NULL;													//Apaga Ponteiro de Objeto
		menu_set(menu_screen);
		objects_manager_select(objs,objs->selected_index);
	}
}

//Desmarca o Objeto Marcardo atual
void objects_manager_unselect(objects_manager * objs){
	if(objs->selected){
		objs->selected->mode&=~OBJ3D_MODE_SELECTED; //Retira Objeto do Modo Selecionado
		objs->selected=NULL;						//Apaga Ponteiro de Objeto
		menu_set(menu_screen);
	}
}

//Seleciona Objeto pelo Indice
void objects_manager_select(objects_manager * objs, int index){
	if(objs->objs_qtd){
		objects_manager_unselect(objs);				//Desmarca qualquer objeto atualmente selecionado
		if(index<0)
			index=objs->objs_qtd+index;		//Lista com indice negativo (como em python)
		else
			index%=objs->objs_qtd;			//Mod para fazer ciclo de indices
		objs->selected=objs->objs[index];   //Ajuda ponteiro do objeto selecionado
		objs->selected->mode|=OBJ3D_MODE_SELECTED;//Coloca Objeto no Modo Selecionado
		objs->selected_index=index;			//Salva index do Objeto Selecionado
		menu_set(menu_object);
	}
}

//Seleciona Proximo Objeto da Lista
void objects_manager_select_next(objects_manager * objs){
	if(objs->selected)					//Se tiver Objeto Selecionado vai para o proximo
		objects_manager_select(objs,objs->selected_index+1);
	else								//Vai para o primeiro
		objects_manager_select(objs,0);
}

//Seleciona Objeto Anterior da Lista
void objects_manager_select_prev(objects_manager * objs){
	objects_manager_select(objs,objs->selected_index-1);//seleciona o anterior ou o ultimo
}



	
